package main

import "fmt"

type Data struct {
	a int
	b int
}

type AddOpt struct {
	Data
}

type SubOpt struct {
	Data
}

func (add *AddOpt) Operate() int {
	return add.a + add.b
}

func (sub *SubOpt) Operate() int {
	return sub.a - sub.b
}

type Opter interface {
	Operate() int
}

type OpFactory struct {
}

//工厂模式
func (obj *OpFactory) OptCalc(a int, b int, op string) (res int) {
	var h Opter
	switch op {
	case "+":
		var add AddOpt = AddOpt{Data{a, b}}
		//res=add.Operate()

		//var h Opter
		h = &add
		//res = h.Operate()
	case "-":
		var sub SubOpt = SubOpt{Data{a, b}}
		//res=sub.Operate()

		//var h Opter
		h = &sub
		//res = h.Operate()
	}
	//res=h.Operate()
	res=factory(h)
	return res
}

//多态
func factory(i Opter) (int) {
	res:=i.Operate()
	return res
}

func main() {
	a := AddOpt{Data{10, 20}}
	b := a.Operate()
	fmt.Println(b)

	c := SubOpt{Data{20, 10}}
	d := c.Operate()
	fmt.Println(d)

	var h Opter
	h = &a
	res := h.Operate()
	fmt.Println(res)

	e := OpFactory{}
	res = e.OptCalc(10, 30, "-")
	fmt.Println(res)

	res1:=Opter.Operate(&a)
	fmt.Println("res1=",res1)
}
